class Index{
    constructor(){
        this.login = document.querySelector(".floor");
        this.welcome = document.querySelector(".welcome");
        this.logout = document.getElementById("logout");
        this.uName = document.getElementById("userName")

        this.init();
        this.addEvent();
    }
    init(){
        this.m = sessionStorage.getItem("userMsg");
        this.m = this.m ? JSON.parse(this.m) : {};
        if(this.m.user){
            this.uName.innerHTML = this.m.user;
            this.login.style.display = "none";
            this.welcome.style.display = "flex";
        }
    }
    addEvent(){
        var that = this;
        this.logout.onclick = function(){
            sessionStorage.removeItem("userMsg");
            location.reload();
        }
    }
}
new Index;

class List{
    constructor(){
        this.url = "http://localhost/project/dest/libs/data/goods.json";
        this.cont = document.querySelector(".wCont");

        this.load();
    }
    load(){
        ajax({
            url:this.url
        }).then((res)=>{
            this.res = JSON.parse(res);
            this.display();
        })
    }
    display(){
        // console.log(this.res);
        let str = "";
        for(var i=0;i<this.res.length;i++){
            str += `<div class="wGoods" index="${this.res[i].goodsId}">
                        <a href="http://localhost/project/dest/pages/goodsdetails.html?id=${this.res[i].goodsId}">
                            <img src="${this.res[i].img.largeImg[0]}" alt="">
                            <p>${this.res[i].name}</p>
                            <ul class="list">
                                ${this.createLi(this.res[i].img.smallImg)}
                            </ul>
                            <h3>￥${this.res[i].price}</h3>
                        </a>
                    </div>`
        }
        this.cont.innerHTML = str;
    }
    createLi(arr){
        let str = "";
        for(let i=0;i<arr.length;i++){
            str += `<li><img src="${arr[i]}" alt=""></li>`
        }
        return str;
    }
}
new List;